package com.sci99.ex.model;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

public class Employee implements UserDetails {
	private Long id;
	private String realname;
	private String departmentStr;
	private String deptID;
	private String empNumber;
	private String dutyMemo;
	private String phone;
	private byte[] photo;
	private Long eid;
	private String pwd;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public String getDepartmentStr() {
		return departmentStr;
	}

	public void setDepartmentStr(String departmentStr) {
		this.departmentStr = departmentStr;
	}

	public String getDeptID() {
		return deptID;
	}

	public void setDeptID(String deptID) {
		this.deptID = deptID;
	}

	public String getEmpNumber() {
		return empNumber;
	}

	public void setEmpNumber(String empNumber) {
		this.empNumber = empNumber;
	}

	public String getDutyMemo() {
		return dutyMemo;
	}

	public void setDutyMemo(String dutyMemo) {
		this.dutyMemo = dutyMemo;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public byte[] getPhoto() {
		return photo;
	}

	public void setPhoto(byte[] photo) {
		this.photo = photo;
	}

	public BufferedImage getImage() {
		if (photo == null)
			return null;
		ByteArrayInputStream bis = new ByteArrayInputStream(photo);
		Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpg");
		// ImageIO is a class containing static convenience methods for locating
		// ImageReaders
		// and ImageWriters, and performing simple encoding and decoding.
		BufferedImage image = null;
		ImageReader reader = (ImageReader) readers.next();
		Object source = bis; // File or InputStream, it seems file is OK
		try {
			ImageInputStream iis = ImageIO.createImageInputStream(source);
			reader.setInput(iis, true);
			ImageReadParam param = reader.getDefaultReadParam();
			image = reader.read(0, param);
		} catch (Exception e) {
		}
		return image;
	}

	public void setEid(Long eid) {
		this.eid = eid;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<SimpleGrantedAuthority> l = new ArrayList<>();
		if (this.eid == 182l)
			l.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
		l.add(new SimpleGrantedAuthority("ROLE_USER"));
		return l;
	}

	@Override
	public String getPassword() {
		return this.pwd;
	}

	@Override
	public String getUsername() {
		return this.eid.toString();
	}

	/**
	 * 是否未过期
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired()
	 */
	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	/**
	 * 是否未锁定
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked()
	 */
	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return true;
	}

	/**
	 * 是否凭据(密码)未过期
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetails#isCredentialsNonExpired()
	 */
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	/**
	 * 是否启用
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetails#isEnabled()
	 */
	@Override
	public boolean isEnabled() {
		return true;
	}
}
